package com.sls.web.manager.recharge.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.Orders;
import com.sls.web.manager.recharge.querybean.OrdersQuery;
import com.sls.web.manager.recharge.service.OrdersService;
import com.sls.web.manager.socket.SuperService;

@Service("ordersService")
public class OrdersServiceImpl extends SuperService implements OrdersService {
	private Gson gson = CommonUtil.gson();

	@Override
	public Pagination<Orders> findNotPay(OrdersQuery ordersQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.FIND_NOT_PAY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = new SocketDTO();
		dto = super.send(head, ordersQuery);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
		return gson.fromJson(dto.getBody(),
				new TypeToken<Pagination<Orders>>() {
				}.getType());
	}

	@Override
	public void manual(Orders orders, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.MENUAL_ORDERS);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = new SocketDTO();
		dto = super.send(head, orders);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}

	}

	@Override
	public Orders findById(Orders orders, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.FIND_ORDERS_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = new SocketDTO();
		dto = super.send(head, orders);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
		return gson.fromJson(dto.getBody(), Orders.class);

	}

	@Override
	public void delete(Orders orders, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_ORDERS);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = new SocketDTO();
		dto = super.send(head, orders);
		head = dto.getHead();
		if (!Constants.SUCCESS.equals(head.getResponseCode())) {
			throw new ManagerException(head.getResponseMessage());
		}
	}

}
